const host = 'https://www.yandea.com/mpcctp'
//const host = 'http://192.168.10.147:8080'
//const host = 'http://localhost:8089'
//const host='http://192.168.10.130:8080'
// const host = 'http://172.20.200.195:8080'
const toast_duration = 2000
const warning_ico = '/image/ico/warning-ico.png'
const error_ico = '/image/ico/error-ico.png'
const status404 = {
  title: '404',
  image: warning_ico,
  duration: toast_duration
}
const status500 = {
  title: "服务异常",
  image: error_ico,
  duration: toast_duration
}
const statusUnknown = {
  title: "未知错误",
  image: error_ico,
  duration: toast_duration
}
const statusFail = {
  title: '数据请求失败',
  image: warning_ico,
  duration: toast_duration
}
const wxRequest = (params, url) => {
  wx.request({
    url: url,
    method: params.method || 'GET',
    data: params.data || {},
    header: {
      "Content-Type": "application/x-www-form-urlencoded; charset=utf-8"
    },
    success: (res) => {
      switch (res.statusCode) {
        case 200:
          params.success && params.success(res.data);
          break;
        case 404:
          wx.showToast(status404)
          break;
        case 500:
          wx.showToast(status500)
          break;
        default:
          wx.showToast(statusUnknown)
          break;
      }
    },
    fail: (res) => {
      params.fail && params.fail(res)
      wx.showToast(statusFail)
    },
    complete: (res) => {
      params.complete && params.complete(res)
    }
  })
}
//获取用户信息
const getUser = (params) => {
  wx.getStorage({
    key: 'loginInfo',
    success: (res) => {
      console.log("user",res)
      params.done && params.done(res.data.REP_BODY);
    },
    fail: (res) => {
      params.fail && params.fail(res)
      console.log("获取用户信息失败", res)
    },
  })
}
//接口全局定义
// my
const getUserOrderCount = (params) => wxRequest(params, host + '/ME0001')

const getAddressList = (params) => wxRequest(params, host + '/MA0004')
const delAddressItem = (params) => wxRequest(params, host + '/MA0002') //删除地址
const setDefaultAddress = (params) => wxRequest(params, host + '/MA0005') //设置默认地址
const setAddress = (params) => wxRequest(params, host + '/MA0001') //保存地址
const upDataAddress = (params) => wxRequest(params, host + '/MA0003') //更新地址
//index
const indexView = (params) => wxRequest(params, host + '/IE0001') //首页展现
//filter
const _filterIndex = (params) => wxRequest(params, host + '/FT0001') //分类-筛选

const _filterSelect = (params) => wxRequest(params, host + '/FT0002') //点击筛选

//system
const login = (params) => wxRequest(params, host + '/US0001') //登录
const register_0 = (params) => wxRequest(params, host + '/US0002') //注册
const register_1 = (params) => wxRequest(params, host + '/VC0001') //厂商注册
const businessLogin = (params) => wxRequest(params, host + '/VC0002') //厂商登录
const getSms = (params) => wxRequest(params, host + '/US0004') //发送短信
const get_M_Sms = (params) => wxRequest(params, host + '/US0007') //发送短信
const setSms = (params) => wxRequest(params, host + '/US0005') //验证短信
//购物车
const getCart = (params) => wxRequest(params, host + '/CA0003') //购物车列表
const delCart = (params) => wxRequest(params, host + '/CA0002') //删除购物车
const setCart = (params) => wxRequest(params, host + '/CA0001') //添加到购物车
const setCartNumber = (params) => wxRequest(params, host + '/CA0004') //获取购物车数量

//商品列表
const getGoodsList = (params) => wxRequest(params, host + '/GD0011') //商品列表
const getGoodsDetail = (params) => wxRequest(params, host + '/GD0010') //商品详情
const getrecommend = (params) => wxRequest(params, host + '/GD0002') //同系列推荐

const setOrder = (params) => wxRequest(params, host + '/OR0001') //提交订单

const setCollection = (params) => wxRequest(params, host + '/CC0001') //添加收藏商品/店铺
const getCollectionList = (params) => wxRequest(params, host + '/CC0002') //收藏商品列表/店铺列表
const delCollection = (params) => wxRequest(params, host + '/CC0003') //删除收藏商品/店铺

const getOrderDetail = (params) => wxRequest(params, host + '/OR0003') //订单详情
const cancelOrder = (params) => wxRequest(params, host + '/OR0002') //取消订单
const getOrder = (params) => wxRequest(params, host + '/ME0006') //全部订单
const getOrder_0 = (params) => wxRequest(params, host + '/ME0002') //待付款
const getOrder_1 = (params) => wxRequest(params, host + '/ME0003') //待发货
const getOrder_2 = (params) => wxRequest(params, host + '/ME0004') //待收货
const getShop = (params) => wxRequest(params, host + '/VC0006') //店铺首页

const getWareHouse = (params) => wxRequest(params, host + '/VC0003') //获取厂商仓库信息

const getCrowdfunding = (params) => wxRequest(params, host + '/GD0003') //获取众筹列表

const getManufactorOrder = (params) => wxRequest(params, host + '/OR0004') //获取厂商众筹单

const getManufactorOrder2 = (params) => wxRequest(params, host + '/GD0004')

const getOthersOrder = (params) => wxRequest(params, host + '/OR0005') //获取其他订单
const payOrder = (params) => wxRequest(params, host + '/PY0001') //获取其他订单

const getDetailOrder = (params) => wxRequest(params, host + '/OR0008') //获取其他订单

const getRefund = (params) => wxRequest(params, host + '/OR0006') //退款

const getCrowdfundingItem = (params) => wxRequest(params, host + '/OR00011') //我的众筹
const modifyPassword = (params) => wxRequest(params, host + '/US0003') //修改密码

const modifyPassword2 = (params) => wxRequest(params, host + '/VC0010') //修改密码

const confirmOrder = (params) => wxRequest(params, host + '/ME0008') //确认收货

module.exports = {
  status404,
  status500,
  statusUnknown,
  statusFail,
  getUserOrderCount,
  getOrder,
  getAddressList,
  delAddressItem,
  setDefaultAddress,
  setAddress,
  login,
  register_0,
  register_1,
  getSms,
  setSms,
  indexView,
  _filterIndex,
  _filterSelect,
  getCart,
  delCart,
  setCart,
  getGoodsList,
  getGoodsDetail,
  setOrder,
  setCollection,
  getCollectionList,
  delCollection,
  businessLogin,
  getOrderDetail,
  cancelOrder,
  getOrder_0,
  getOrder_1,
  getOrder_2,
  getShop,
  getUser,
  getWareHouse,
  getCrowdfunding,
  upDataAddress,
  getManufactorOrder,
  getManufactorOrder2,
  getOthersOrder,
  setCartNumber,
  payOrder,
  getDetailOrder,
  getRefund,
  getCrowdfundingItem,
  getrecommend,
  modifyPassword,
  get_M_Sms,
  modifyPassword2,
  confirmOrder
}